 export const BASE_URL = 'https://api.devonline.cn'
import store from '../store'

export const myRequest = (url, data = {}, method) => {
	//加载框
	store.commit('switchNetwork', {
		code: true
	})
	var token = uni.getStorageSync("token") || "";
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + url,
			method: method || 'GET', 
			data: data || {},
			header: {
				"XX-Device-Type": "wxapp",
				"Content-Type":"application/json",
				"XX-User-Type": 'user',
				Authorization:`Bearer ${token}`
			},
			success: (res) => {		
				console.log('????',res)
				if (res.statusCode == '200') {
					resolve(res.data)
				} else if (res.statusCode == '401') {
					uni.showToast({
						title: '您还未认证！',
						icon: "none"
					})
					uni.redirectTo({
						url:'/pages/login/login'
					})
				}else {
					uni.showToast({
						title: res.data,
						icon: "none"
					})
				}
			},
			fail: (err) => {	
				console.log('错误',err)
				uni.showToast({
					title: '请求失败，请检查网络！', 
					icon: "none"
				})
				reject(err.data)
			},
			complete:()=>{
				store.commit('switchNetwork', {
					code: false
				})
			}
		})
	}).catch((e) => {})
}
